/**
 * Copyright 2007 Christian Mueller
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
 * use this file except in compliance with the License. You may obtain a copy of 
 * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 
 * applicable law or agreed to in writing, software distributed under the License 
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 * either express or implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 * 
 * Description:
 * This is the main startup routine
 */
 
 #include "myglobal.h"
 #include "logging.h"
 #include "options.h"
 #include "globopts.h"
 #include "execute_agent.h"
 #include "replicid.h"
 #include "ncopy.h"
 #include "nbackup.h"
 
 void usage_examples() {
 	printf("Examples:\n");
 	printf("nSwiss --execute-agent TestAgent --database testdb.nsf\n");
 	printf("This will execute the agent 'TestAgent' in the database 'testdb.nsf'\n\n"); 
 	printf("nSwiss --get-replicid --database names.nsf\n");
 	printf("This will display the replicid of the database 'names.nsf'\n\n");
 	printf("nSwiss --create-replicid --database names.nsf\n");
 	printf("This will create a new replicid for the database 'names.nsf'\n\n");
 	printf("nSwiss --copy-replicid --source-database source.nsf --dest-database dest.nsf\n");
 	printf("Copy the replicid of 'source.nsf' to 'dest.nsf'\n\n");
 	printf("nSwiss --copy-database --source-database names.nsf --file c:\\names.nsf\n");
 	printf("Copy the database 'names.nsf' online to c:\\names.nsf without corruption.\n\n");
 	printf("nSwiss ----backup-databases --dir c:\\notesbackup\n");
 	printf("Makes an online copy of all databases and no templates to the directory c:\\notesbackup\n\n");
 }
 
 
 void usage() {
 	printf("\nnSwiss.exe - A swiss army knife command tool for lotus notes\n\n");
 	printf("(C) 2007 by Christian Mueller (Apache License, Version 2.0)\n");
 	printf("This tool is a collection of small usefull tools, for lotus notes\n");
 	printf("With the execution commands you can trigger an action. Please only\n");
 	printf("use only one execute option!\n");
 	options_printusage(nswiss_options);
 	printf("\n");
 	usage_examples();
 	printf("\n");	
 }
 int main(int argc, char *argv[]) {
 	
	/** Starting parsing the options */
	//usage();
	if (options_parse(nswiss_options,argc, argv,1)!=0) {
		usage();
		return 8;	
	}
	
	// Initialise Notes...
	// check first, if a notes.ini is defined
	if (opt_notesini[0]!='\0') {
		// Ok open with notes.ini
		char *parms[2];
		parms[0]=argv[0];
		parms[1]=opt_notesini;
		if (NotesInitExtended (2, parms))
		{
     		printf("\n Unable to initialize Notes.\n");
     		return (1);
		}
	} else {
		if (NotesInit())
		{
     		printf("\n Unable to initialize Notes.\n");
     		return (1);
		}
	}
	logging_set_applicationName("nSwiss");
	if (opt_verbose == TRUE) {
		// printf("Running verbose...\n");
		logging_set_loglevel(LOG_DEBUG0); // Set verbose level
	} else if (opt_debuglevel>0) {
		// printf("Set Debuglevel to %d\n",opt_debuglevel);
		logging_set_loglevel(opt_debuglevel);
	}
	
	LOG_DEBUG0("Check command to execute:\n");
	if (opt_help == TRUE ) {
		usage();
	} else if (opt_executeAgent[0]!='\0') {
		LOG_DEBUG2("call run_execute_agent()");
		run_execute_agent();
	} else if (opt_getreplicId == TRUE) {
		LOG_DEBUG2("call run_get_replic_id()");
		run_get_replic_id();
	} else if (opt_createreplicId == TRUE) {
		LOG_DEBUG2("call run_create_replic_id()");
		run_create_replic_id();
	} else if (opt_copyreplicId == TRUE) {
		LOG_DEBUG2("call run_copy_replic_id()");
		run_copy_replic_id();
	} else if (opt_copynotesdb == TRUE) {
		LOG_DEBUG2("call run_copy_notesdb()");
		run_copy_notesdb();
	} else if (opt_backupdb == TRUE) {
		LOG_DEBUG2("call run_make_backup()");
		run_make_backup();
	} else
		LOG_ERROR("No command found!\n");	
	LOG_DEBUG0("Terminate Notes Session!\n");
 	NotesTerm();
 	return 0;
 }
